﻿using System;
using Autofac;
using EP.Base.BaseService.Manager.Business.Task;
using EP.Base.BaseService.Manager.Config;
using EP.Base.BaseService.Manager.Interface.Business;
using EP.Base.BaseService.Manager.Model.Entity;

namespace EP.Base.BaseService.Manager.Business
{
    public class EsIndexHandelTaskRepertory : IEsIndexHandelTaskRepertory
    {
        private readonly IContainer _container;

        public EsIndexHandelTaskRepertory(IContainer container)
        {
            _container = container;
            //if (_container == null)
            //{
            //    var containerBuilder=new ContainerBuilder();
                //删除
                //containerBuilder.RegisterType<DeleteIndexTask>().As<IEsIndexHandelTask>().Named<IEsIndexHandelTask>("DeleteIndexTask").SingleInstance();
                //备份
                //containerBuilder.RegisterType<BackupIndexTask>().As<IEsIndexHandelTask>().Named<IEsIndexHandelTask>("BackupIndexTask").SingleInstance();

            //    _container = containerBuilder.Build();
            //}
        }

        public IEsIndexHandelTask Get(EsTaskConfig esTaskConfig)
        {
            if(_container==null)
                throw new Exception("IContainer容器未初始化!");
            return _container.ResolveNamed<IEsIndexHandelTask>(GetServiceNameByConfigType(esTaskConfig.Type));
        }

        private string GetServiceNameByConfigType(short type)
        {
            var serviceConfig = IndexTaskServiceConfigHelper.IndexConfig(type);
            return serviceConfig.ServiceName;
        }
    }
}
